package com.cande.loki.exercise.algorithm;

//import com.alibaba.druid.sql.visitor.functions.Char;

import java.util.Arrays;
import java.util.Objects;
import java.util.Stack;

/**
 * @BelongsProject: loki
 * @BelongsPackage: com.cande.loki.exercise.algorithm
 * @Author: WANGAOFEI
 * @CreateTime: 2023-10-20  16:08
 * @Description: 反转str 链表
 * @Version: 1.0
 */
public class ReverseArray {
    public static void main(String[] args) {
        // 反转string
        String str = "abc";
        String [] arr = str.split("");
        Stack<String> stack = new Stack<>();
        Arrays.stream(arr).forEach(stack::push);
        StringBuilder sb = new StringBuilder();
        while (!stack.isEmpty()) {
            sb.append(stack.pop());
        }
        System.out.println(sb.toString());
        //反转数组

        //反转链表
        ListNode node = new ListNode();
        node.setData("!");
        ListNode node1 = new ListNode();
        node1.setData("2");

        ListNode node2 = new ListNode();
        node2.setData("3");

        node.next=node1;
        node1.next=node2;

        Stack<ListNode> stackNode = new Stack<>();
        while (node != null){
            stackNode.push(node);
            node=node.next;
        }
        node = stackNode.pop();
        ListNode temp = node;
        while (!stackNode.isEmpty()){
            ListNode nodenew =stackNode.pop();
            nodenew.next =null;
            temp.next = nodenew;
            temp=temp.next;

        }
        System.out.println(node);
    }

    public ListNode reverse(ListNode head)
    {
        Stack<ListNode> stack = new Stack<>();
        while (head != null){
            stack.push(head);
            head = head.next;
        }

        head = stack.pop();
        ListNode temp1 = head;
        while (!stack.isEmpty()){
            ListNode temp = stack.pop();
            temp.next =null;
            temp1.next = temp;
            temp1=temp1.next;
        }

        return head;
    }
}
